<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="年级">
              <a-select allowClear v-model="queryParam.ssnj">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="培养模式">
                <a-select allowClear v-model="queryParam.pymsm" @change="pymsChange">
                  <a-select-option v-for="i in TYPE0347" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12" v-if="queryParam.pymsm === '01'">
              <a-form-item label="门类">
                <a-select allowClear v-model="queryParam.mlxkdm" showSearch :filterOption="filterOption" @change="subjectChange">
                  <a-select-option v-for="i in selectMenu.subjectList" :key="i.id" :value="i.xkdm">{{i.xkdm}}-{{ i.xkmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12" v-if="queryParam.pymsm === '01'">
              <a-form-item label="一级学科">
                <a-select allowClear v-model="queryParam.xkdm" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.subSubjectList" :key="i.id" :value="i.xkdm">{{i.xkdm}}-{{ i.xkmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12" v-if="queryParam.pymsm === '02'">
              <a-form-item label="领域">
                <a-select allowClear v-model="queryParam.xkdm" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.subjectList" :key="i.id" :value="i.xkdm">{{i.xkdm}}-{{ i.xkmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!--            <a-col :md="6" :sm="12">-->
            <!--              <a-form-item label="培养来源">-->
            <!--                <a-select allowClear v-model="queryParam.xslxm">-->
            <!--                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>-->
            <!--                </a-select>-->
            <!--              </a-form-item>-->
            <!--            </a-col>-->
            <a-col :md="6" :sm="12">
              <a-form-item label="培养类别">
                <a-select allowClear v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <a-button type="primary" @click="batchDelete" style="margin:0 10px">删除</a-button>
              <a-button type="primary" @click="exportList" style="margin:0 10px">导出</a-button>
              <a-button type="primary" @click="batchPrint" style="margin:0 10px">打印</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :scroll="{ x: 1600 }"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="operation" slot-scope="a,b">
          <a @click="showDetail(b)">查看明细</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="toEdit(b)">修改</a>
              </a-menu-item>
              <!-- <a-menu-item>
                <a @click="deleteSch(b)">删除</a>
              </a-menu-item> -->
              <a-menu-item>
                <a @click="triggerSch(b)">{{ b.fazt === '01' ? '停用' : '启用' }}</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          width: 80,
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '所属年级',
          dataIndex: 'ssnjmc'
        },
        {
          title: '培养方案',
          dataIndex: 'pyfamc',
          width:260
        },
        {
          title: '学科/领域代码',
          dataIndex: 'xkzym'
        },
        {
          title: '学科/领域名称',
          dataIndex: 'xkmc'
        },
        {
          title: '类别',
          dataIndex: 'xklbmc',
          width: 100
        },
        // {
        //   title: '培养来源',
        //   width: 100,
        //   dataIndex: 'xslxmc'
        // },
        {
          title: '培养类别',
          dataIndex: 'xslbmc',
          width: 120
        },
        {
          title: '培养模式',
          width: 100,
          dataIndex: 'pymsmc'
        },
        {
          title: '状态',
          width: 80,
          dataIndex: 'faztmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '160px',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.schemeList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0347: state => state.app['TYPE0347'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0075: state => state.app['TYPE0075']
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if (from.matched[0] && from.matched[0].name === '静态路由' && pageParams.path === vm.$route.path) {
        vm.pymsChange(pageParams.params.pymsm, 0)
        if (pageParams.params.mlxkdm) {
          vm.subjectChange(pageParams.params.mlxkdm)
        }
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
        vm.init(1)
      } else {
        vm.initSelectMenu('01')
        vm.init()
      }
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.matched[0].name === '静态路由') {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0347')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    // this.initSelectMenu('01')
  },
  methods: {
    init(noFirst = false) {
      this.$store.dispatch('app/sysInfo').then(data => {
        if (!noFirst) {
          this.$set(this.queryParam, 'ssnj', data.currentGrade)
        }
        this.initDone = true
      })
    },
    initSelectMenu (xklb) {
      let allRequest = [
        this.$api.base.subjectAll({xklb: xklb}),
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          subjectList: res[0].data
        }
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    toEdit (s) {
      this.$router.push(`/cultivate/scheme/add?id=${s.id}`)
    },
    showDetail (t) {
      this.$router.push(`/cultivate/scheme/detail/${t.id}`)
    },
    pymsChange(code, reset = 1){
      if (reset) {
        this.$set(this.queryParam, 'xkdm', '')
        this.$set(this.queryParam, 'mlxkdm', '')
      }
      this.initSelectMenu(code === '01' ? '01' : '04')
    },
    subjectChange (code) {
      this.$set(this.queryParam, 'xkdm', '')
      if (code) {
        this.$api.base.subjectAll({fjxkdm: code, xklb: '02'}).then(res => {
          this.$set(this.selectMenu, 'subSubjectList', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'subSubjectList', [])
      }
    },
    // subjectChange (code) {
    //   this.$set(this.queryParam, 'yjkx', '')
    //   if (code) {
    //     this.$api.base.subjectAll({fjxkdm: code, xklb: '02'}).then(res => {
    //       this.$set(this.selectMenu, 'subSubjectList', res.data)
    //     })
    //   } else {
    //     this.$set(this.selectMenu, 'subSubjectList', [])
    //   }
    // },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的培养方案')
        return
      }
      this.$confirm({
        title: '确定删除所选中的培养方案?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除',
        centered: true,
        onOk: () => {
          let ids = this.selectedRowKeys.join(',')
          this.$api.cultivate.schemeDelete(ids).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => { }
      })
    },
    triggerSch (s) {
      let apiFn = s.fazt === '01' ? 'schemeStop' : 'schemeStart'
      this.$confirm({
        title: '提示',
        content: `确定${s.fazt === '01' ? '停用' : '启用'}课程信息吗？`,
        centered: true,
        onOk: () => {
          this.$api.cultivate[apiFn](s.id).then((res) => {
            this.$message.success(res.msg)
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => { }
      })
    },
    batchPrint () {
      if (!this.selectedRowKeys.length) {
        return this.$message.warning('请选择需要打印的培养方案')
      }
      this.$export('/foster/pyfa/batchPrint', {
        ids: this.selectedRowKeys.join(',')
      })
    },
    exportList () {
      let params = this.queryParam
      delete params.pageNum
      this.$export('/foster/pyfa/listExp', {
        ...params
      })
    }
  }
}
</script>

<style>

</style>
